<?php

class Home extends Controller {

	// Constructor
	function Home()
    {
       
		parent::Controller();
		
		// load model
		$this->load->model('Df602_model', 'df602', TRUE);
		
		// select table to scaff
		$this->load->scaffolding('realtime');
		
		// load session lib
		$this->load->library('session');
		
		$this->load->helper('form');
		
		

    }

	function index()
	{	
		$this->initNavigater();
		
		$clientList = $this->df602->get_all_client_info();
		
		$counter = 0;
		foreach ($clientList as $row)
		{
	      $data['clientList'][$counter] = $row;
		  $counter++;
		}
		
		$counter = 0;
		foreach ($clientList as $row)
		{
			$hdList = $this->df602->get_client_info($row->id);
			
			$counter2 = 0;
			foreach ($hdList as $row2)
			{
				if($row2->size > 0)
				{
					$row2->percentUsed = $row2->used / $row2->size * 100;
					$row2->percentAvailable = $row2->available / $row2->size * 100;
					$row2->percentUsed_for_generate_bar = round($row2->percentUsed / 2);
					$row2->percentBar = $this->generatePercentBar($row2->percentUsed, 60, 90);
					
					$data['svhdList'][$counter][$counter2] = $row2;
					$counter2++;
				}
			}
			$counter++;
		}
		
		// generate navigater
		$data['navigater'] = $this->generateNavigater();
		 
		$this->load->view('homeView', $data);
	}
	
	
	
	function clientInfo($clientID)
	{	
		if(!$this->session->userdata('login'))
		{
			return;
		}
		$clientInfo = $this->df602->get_client_info($clientID);
		
		$counter = 0;
		foreach($clientInfo as $row)
		{
			if($row->size > 0)
			{
				$row->percentUsed = $row->used / $row->size * 100;
				$row->percentAvailable = $row->available / $row->size * 100;
				$row->percentUsed_for_generate_bar = round($row->percentUsed / 2);
				$row->percentBar = $this->generatePercentBar($row->percentUsed, 50, 90);
				
				// change unit
				$row->used = $this->unitChange($row->used);
				$row->available = $this->unitChange($row->available);
				$row->size = $this->unitChange($row->size);
				
				$pos = 0;
				if(strrpos($row->device, "/")) $pos = strrpos($row->device, "/")+1;
				$row->deviceShortName = substr($row->device, $pos);
				
				$row->devicePassName = str_replace("/", "_", $row->device);

				
				$data['hdList'][$counter] = $row;
				$counter++;
			}
		}
		
		//echo $this->generatePercentBar(30, 50, 50);
		
		if($clientInfo)
		$this->load->view('clientInfoView', $data);
	}
	
	function listAllClient()
	{
		if(!$this->session->userdata('login'))
		{
			return;
		}
		
		$clientList = $this->df602->get_all_client_info();
		
		$counter = 0;
		foreach ($clientList as $row)
		{
	      $data['clientList'][$counter] = $row;
		  $counter++;
		}
		if($data)
		$this->load->view('clientListView', $data);
	}
	
	function listAllClientHD()
	{
		if(!$this->session->userdata('login'))
		{
			$this->load->view('loginView');
			return;
		}
		
		if(!$this->session->userdata('login'))
		{
			$this->load->view('loginView');
			return;
		}
		
		$clientList = $this->df602->get_all_client_info();
		
		$counter = 0;
		foreach ($clientList as $row)
		{
	      $data['clientList'][$counter] = $row;
		  $counter++;
		}
		
		$counter = 0;
		foreach ($clientList as $row)
		{
			$hdList = $this->df602->get_client_info($row->id);
			
			$counter2 = 0;
			foreach ($hdList as $row2)
			{
				if($row2->size > 0)
				{
					$row2->percentUsed = $row2->used / $row2->size * 100;
					$row2->percentAvailable = $row2->available / $row2->size * 100;
					$row2->percentUsed_for_generate_bar = round($row2->percentUsed / 2);
					$row2->percentBar = $this->generatePercentBar($row2->percentUsed, 60, 90);
					
					$data['svhdList'][$counter][$counter2] = $row2;
					$counter2++;
				}
			}
			$counter++;
		}
		

		$this->load->view('listAllClientHD', $data);
	}
	
	function hdInfo($clientID, $hdName)
	{
		if(!$this->session->userdata('login'))
		{
			return;
		}
		
		$hdName = str_replace("_", "/", $hdName);
		$hdInfo = $this->df602->get_hd_info($clientID, $hdName);
		foreach($hdInfo as $row)
		{
			$row->percentUsed = $row->used / $row->size * 100;
			$row->percentAvailable = $row->available / $row->size * 100;
			$row->percentUsed_for_generate_bar = round($row->percentUsed / 2);
			$row->percentBar = $this->generatePercentBar($row->percentUsed, 50, 90);
			
			// change unit
			$row->used = $this->unitChange($row->used);
			$row->available = $this->unitChange($row->available);
			$row->size = $this->unitChange($row->size);
			
			$pos = 0;
			if(strrpos($row->device, "/")) $pos = strrpos($row->device, "/")+1;
			$row->deviceShortName = substr($row->device, $pos);
			
			$row->devicePassName = str_replace("/", "_", $row->device);

			$data['hdInfo'] = $row;
		}
		
		if($data)
		$this->load->view('hdInfoView', $data);
	
	}
	
	function generatePercentBar($percent, $numBar, $dangerRate)
	{
		if(!$this->session->userdata('login'))
		{
			return;
		}
		
		$color = '';
		$colorSet = array("#A0B60A", "#427706", "#158E04", "#FF6600", "#FB2904", "#DB040A");
		$regularCon = $dangerRate;
		$step = $regularCon / 5.0;
		for($i=0; $i<5; $i++)
		{
			$state[$i] = ($i+1) * $step;
		}

		
		if($percent <= $state[0]) $color = $colorSet[0];
		else if($percent <= $state[1]) $color = $colorSet[1];
		else if($percent <= $state[2]) $color = $colorSet[2];
		else if($percent <= $state[3]) $color = $colorSet[3];
		else if($percent <= $state[4]) $color = $colorSet[4];
		else if($percent <= 100) $color = $colorSet[5];
		
		$numPercentBar = round(($numBar * $percent) / 100);
		$remainBar = $numBar - $numPercentBar;
		
		$reslt = '';
		
		$reslt .= "<font color='".$color."'>";
		for($i=0; $i<$numPercentBar; $i++) $reslt .= "|"; 
		$reslt .= "</font>";
		
		$reslt .= "<font color='#DDDDDD'>";
		for($i=0; $i<$remainBar; $i++) $reslt .= "|"; 
		$reslt .= "</font>";
		
		return $reslt;
	}
	
	
	function initNavigater()
	{
		if(!$this->session->userdata('login'))
		{
			return;
		}
		
		$index->url = "./home/listAllClientHD";
		$index->name = "Home";
		$index->layer = 0;
		$navArray = array($index);
		$this->session->set_userdata('navigater', $navArray);
	}
	
	function generateNavigater()
	{
		if(!$this->session->userdata('login'))
		{
			return;
		}
		
		$navigater = '';
		$navigateArray = $this->session->userdata('navigater');
		
		for($i=0; $i<count($navigateArray); $i++)
		{
			$navigater .= "<a onClick=\"if(refresher!= '') refresher.stop(); refresher = new Ajax.PeriodicalUpdater($('rightContainer'), '".$navigateArray[$i]->url."', {asynchronous:true, frequency:3}); ";
			$navigater .= "new Ajax.Updater($('navigate'), './home/rewriteNavigater', {parameters: { url: '".$navigateArray[$i]->url."', name: '".$navigateArray[$i]->name."', layer: '".$navigateArray[$i]->layer."' }}); \">".$navigateArray[$i]->name."</a>"; 
			if($i<count($navigateArray)-1) $navigater .= " | ";
		}
		
		return $navigater;
	}
	
	function rewriteNavigater()
	{
		if(!$this->session->userdata('login'))
		{
			return;
		}
		$url = $_POST['url'];
		$name = $_POST['name'];
		$layer = $_POST['layer'];

		$dummy = -1;
		$navigateArray = $this->session->userdata('navigater');
		
		$navigateArray = array_slice($navigateArray, 0, $layer);
		$item->url = $url;
		$item->name = $name;
		$item->layer = $layer;
		array_push($navigateArray, $item);
		$this->session->set_userdata('navigater', $navigateArray);
		
		echo $this->generateNavigater();  
	}
	
	function unitChange($num)
	{
		$strNum = '';
		if($num >= 1024*1024) 
		{
			$strNum = round($num/(1024*1024), 2);
			$strNum .= " GB";
		}
		else if($num >= 1024) 
		{
			$strNum = round($num/1024, 2);
			$strNum .= " MB";
		}
		else $strNum = round($num, 2)." KB";
		
		return $strNum;
	}
	

}











?>